﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CsGL.OpenGL;
using GMPP.DisplayElements;
using GMPP.utils;
using SHVS.Components;
using SHVS.DisplayData;
using SHVS.GeneralTools;
using VLIB28D.Geometry28D.textures;
using VLIB28D.Geometry3D;
using VLIB2D.Geometry2D;

namespace GMPP
{
    using Tx = Texture.Texture;

    public class Scene : OpenGLControl
    {
        #region Camera Para

        public float MouseWheelRatio = 20f;

        public float MoveOnXz = 10f;

        public float MoveOnY = 30;

        #endregion

        // Lighting components for the cube

        public bool Blend = false; // Blending OFF/ON?
        public bool Bp = false; // B Pressed?
        public int Filter = 0; // Which Filter To Use
        public bool Finished;
        public bool Fp = false; // F Pressed?

        public float Heading = 0.0f;
        public float[] LightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
        public float[] LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
        public float[] LightPosition = { 474.31697396969f, 30, 470.792606274295f };
        public float Lookupdown = 0.0f;
        public Dictionary<string, uint> SceneTexs;


        public List<uint> TexList = new List<uint>();
        public List<string> Texfile = new List<string>();
        public string Texpath = string.Empty;
        public uint[] Texture = new uint[3]; // Texture array
        public float Walkbias = 0;
        public float Walkbiasangle = 0;
        public float Xpos = 0.0f;
        public float Yrot = 0.0f; // Y Rotation
        public float Z = 0.0f; // Depth Into The Screen
        public float Zpos = 0.0f;
        private MeshBox _box = new MeshBox();
        private CameraControl _cc;
        private int _dX, _dY;
        private double _fovy = 45;
        private float _ha;
        private bool _isDown;
        private bool _isIn;

        private Point3D _midPt = new Point3D();
        private float _va;

        #region Display data

        private readonly Triangles5D _triangles5D = new Triangles5D();
        private List<FaceModel> _FaceModels = new List<FaceModel>();
        private List<Mesh> _meshes = new List<Mesh>();
        private double _uM;
        private double _vM;

        private double _xM;
        private double _yM;
        private double _zM;

        private double _xV;
        private double _yV;
        private double _zV;
        #endregion

        public Scene(string pathStr)
        {
            Texpath = pathStr;
            SceneTexs = new Dictionary<string, uint>();
            KeyDown += LessonView_KeyDown;
            KeyUp += LessonView_KeyUp;
            MouseDown += Scene_MouseDown;
            MouseMove += Scene_MouseMove;
            MouseUp += Scene_MouseUp;
            MouseWheel += Scene_MouseWheel;
            MouseEnter += Scene_MouseEnter;
            MouseLeave += Scene_MouseLeave;
            Finished = false;
        }

        public List<Mesh> Meshes
        {
            get { return _meshes; }
            set { _meshes = value; }
        }

        public MeshBox Box
        {
            get { return _box; }
            set { _box = value; }
        }

        public Point3D MidPt
        {
            get
            {
                _midPt.X = (_box.Xmax + _box.Xmin) / 2;
                _midPt.Y = (_box.Ymax + _box.Ymin) / 2;
                _midPt.Z = (_box.Zmax + _box.Zmin) / 2;
                return _midPt;
            }
            set { _midPt = value; }
        }

        public CameraControl Cc
        {
            get { return _cc; }
            set { _cc = value; }
        }

        public double Fovy
        {
            get { return _fovy; }
            set { _fovy = value; }
        }

        [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
        public static extern void ShowCursor(int status);

        [DllImport("user32.dll", EntryPoint = "SetCursorPos", CharSet = CharSet.Auto)]
        private static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll", EntryPoint = "ClientToScreen", CharSet = CharSet.Auto)]
        private static extern bool ClientToScreen(IntPtr hwnd, ref Point lpPoint);

        private void Scene_MouseLeave(object sender, EventArgs e)
        {
            _isIn = false;
        }

        private void Scene_MouseEnter(object sender, EventArgs e)
        {
            _isIn = true;
        }

        private void Scene_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                _cc.ZoomOut(MouseWheelRatio);
            }
            else
            {
                _cc.ZoomIn(MouseWheelRatio);
            }
        }

        private void Scene_MouseUp(object sender, MouseEventArgs e)
        {
            _isDown = false;
            IntPtr handle = Handle;
            var pt = new Point();
            bool fsf = ClientToScreen(handle, ref pt);
            ShowCursor(1);
            if (fsf)
            {
                SetCursorPos(pt.X + _dX, pt.Y + _dY);
            }
        }

        private void Scene_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDown && _isIn && (e.Location.X - _dX != 0 || e.Location.Y - _dY != 0))
            {
                _ha = e.Location.X - _dX;
                _va = e.Location.Y - _dY;
                if (_ha > 0)
                {
                    _cc.RatateHorizontal(Convert.ToSingle(Math.PI * _ha * _fovy / Width / 1800));
                }
                else
                {
                    _cc.RatateHorizontal(Convert.ToSingle(Math.PI * _ha * _fovy / Width / 1800));
                }
                if (_va > 0)
                {
                    _cc.RatateVertical(Convert.ToSingle(Math.PI * _va * _fovy / Width / 1800));
                }
                else
                {
                    _cc.RatateVertical(Convert.ToSingle(Math.PI * _va * _fovy / Width / 1800));
                }
            }
        }

        private void Scene_MouseDown(object sender, MouseEventArgs e)
        {
            ShowCursor(0);
            //Meshes.Add(new Mesh());
            _isDown = true;
            _dX = e.X;
            _dY = e.Y;
        }

        protected override void InitGLContext()
        {
            //LoadTextures(texfile);
            //GL.glEnable(GL.GL_TEXTURE_2D);									// Enable Texture Mapping
            //GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);						// Set The Blending Function For Translucency
            //GL.glShadeModel(GL.GL_SMOOTH);									// Enable Smooth Shading
            //GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);						// Black Background
            //GL.glClearDepth(1.0f);											// Depth Buffer Setup
            //GL.glEnable(GL.GL_DEPTH_TEST);									// Enables Depth Testing
            //GL.glDepthFunc(GL.GL_LESS);										// The Type Of Depth Testing To Do
            //GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);		// Really Nice Perspective Calculations


            ////GL.glOrtho(-10000, 10000, -10000, 10000, 0.1, 10000);
            //if (this.blend)													// If blending, turn it on and depth testing off
            //{
            //    GL.glEnable(GL.GL_BLEND);
            //    GL.glDisable(GL.GL_DEPTH_TEST);
            //}
            //SetupPointers();
            LoadTextures(Texfile, Texpath);
            OpenGL.glEnable(OpenGL.GL_TEXTURE_2D);
            //OpenGL.glTexEnvfv(OpenGL.GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, @ColorRGBA);
            OpenGL.glBlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE);
            OpenGL.glShadeModel(OpenGL.GL_SMOOTH); // enable smooth shading
            OpenGL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // black background
            OpenGL.glClearDepth(1.0f); // depth buffer setup
            OpenGL.glEnable(OpenGL.GL_DEPTH_TEST); // enables depth testing
            OpenGL.glDepthFunc(OpenGL.GL_LEQUAL); // type of depth test
            OpenGL.glHint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST); // nice perspective calculations

            GL.glLightfv(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, LightAmbient); // Setup The Ambient Light
            GL.glLightfv(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
            GL.glLightfv(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, LightPosition); // Position The Light
            OpenGL.glEnable(OpenGL.GL_LIGHT1);
            _cc = new CameraControl(new Point3D(MidPt.X - 10, MidPt.Y + 2000, MidPt.Z), MidPt, new Vector3D());


        }

        public bool LoadTextures(List<String> file, string path)
        {
            //file.Add("sky.jpg");
            //file.Add("terrain_texture.jpg");
            var tx = new Tx(file, path, OpenGL.GL_LINEAR_MIPMAP_NEAREST, out SceneTexs);
            Texture = tx.texture;
            return true;
        }

        public void AddMesh(Mesh mesh)
        {
            _box.Xmax = _box.Xmax > mesh.Box.Xmax ? _box.Xmax : mesh.Box.Xmax;
            _box.Ymax = _box.Ymax > mesh.Box.Ymax ? _box.Ymax : mesh.Box.Ymax;
            _box.Zmax = _box.Zmax > mesh.Box.Zmax ? _box.Zmax : mesh.Box.Zmax;

            _box.Xmin = _box.Xmin < mesh.Box.Xmin ? _box.Xmin : mesh.Box.Xmin;
            _box.Ymin = _box.Ymin < mesh.Box.Ymin ? _box.Ymin : mesh.Box.Ymin;
            _box.Zmin = _box.Zmin < mesh.Box.Zmin ? _box.Zmin : mesh.Box.Zmin;
            foreach (string t in mesh.TesStrs)
            {
                if (!Texfile.Contains(t))
                {
                    Texfile.Add(t);
                }
            }


            _meshes.Add(mesh);
        }

        public void AddTriangles5D(Triangles5D t5Ds)
        {

            _triangles5D.Add(t5Ds.Triangles);
            _triangles5D.GetBox();
            _box.Xmax = _box.Xmax > _triangles5D.Box.Xmax ? _box.Xmax : _triangles5D.Box.Xmax;
            _box.Ymax = _box.Ymax > _triangles5D.Box.Ymax ? _box.Ymax : _triangles5D.Box.Ymax;
            _box.Zmax = _box.Zmax > _triangles5D.Box.Zmax ? _box.Zmax : _triangles5D.Box.Zmax;

            _box.Xmin = _box.Xmin < _triangles5D.Box.Xmin ? _box.Xmin : _triangles5D.Box.Xmin;
            _box.Ymin = _box.Ymin < _triangles5D.Box.Ymin ? _box.Ymin : _triangles5D.Box.Ymin;
            _box.Zmin = _box.Zmin < _triangles5D.Box.Zmin ? _box.Zmin : _triangles5D.Box.Zmin;
            foreach (var t in t5Ds.Triangles)
            {
                if (!Texfile.Contains(t.Material))
                {
                    Texfile.Add(t.Material);
                }
            }


        }

        private float angle = 0f;

        public override void glDraw()
        {
            OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            // Clear the Screen and the Depth Buffer

            // angle += 0.01f;
            // Cc.RatateVertical(angle);
            //
            //DrawCoords();
            //OpenGL.glMatrixMode(OpenGL.GL_MODELVIEW); // Modelview Matrix
            OpenGL.glLoadIdentity();
            // GL.gluPerspective(45, 90, 0.01, 10000000000);

            // GL.glTranslated(-midPt.X, 0, -midPt.Z);
            //GL.glRotatef(this.lookupdown, 1.0f, 0.0f, 0.0f);
            // GL.glRotatef(Ha, 0.0f, 1.0f, 0.0f);
            ////GL.glRotatef(Va, 1f, 0, 0);
            //GL.glRotatef(360.0f - this.yrot, 0.0f, 1.0f, 0.0f);
            //GL.glScalef(sx, sy, sz);
            //GL.glTranslatef(-this.xpos, -this.walkbias - 0.25f, -this.zpos);
            GLU.gluLookAt(_cc.CameraLoc.X, _cc.CameraLoc.Y, _cc.CameraLoc.Z, _cc.ViewPort.X, _cc.ViewPort.Y,
                          _cc.ViewPort.Z, 0, 1, 0);

            //Debug.WriteLine(_cc);



            //DrawMeshes();
             //DrawTriangles();
            DrawHeightmap();
            //DrawSky();
            //DrawPyramid();
        }

        private void DrawPyramid()
        {
            OpenGL.glLoadIdentity(); // reset the current modelview matrix
            // move 1.5 Units left and 6 Units into the screen
            GLU.gluLookAt(_cc.CameraLoc.X, _cc.CameraLoc.Y, _cc.CameraLoc.Z, _cc.ViewPort.X, _cc.ViewPort.Y,
                          _cc.ViewPort.Z, 0, 1, 0);
            OpenGL.glBegin(OpenGL.GL_TRIANGLES); // start drawing a triangle, always counterclockside (top-left-right)
            OpenGL.glColor3f(1.0f, 0.0f, 0.0f); // Red
            OpenGL.glVertex3f(0.0f, 2.0f, 0.0f); // Top of Triangle (Front)
            OpenGL.glColor3f(0.0f, 1.0f, 0.0f); // green
            OpenGL.glVertex3f(-2.0f, -2.0f, 6.0f); // left of Triangle (front)
            OpenGL.glColor3f(0.0f, 0.0f, 1.0f); // blue
            OpenGL.glVertex3f(1.0f, -1.0f, 1.0f); // right of triangle (front)

            OpenGL.glColor3f(1.0f, 0.0f, 0.0f); // red
            OpenGL.glVertex3f(0.0f, 1.0f, 0.0f); // top of triangle (right)
            OpenGL.glColor3f(0.0f, 0.0f, 1.0f); // blue
            OpenGL.glVertex3f(1.0f, -1.0f, 1.0f); // left of triangle (right)
            OpenGL.glColor3f(0.0f, 1.0f, 0.0f); // green
            OpenGL.glVertex3f(1.0f, -1.0f, -1.0f); // right of triangel (right)

            OpenGL.glColor3f(1.0f, 0.0f, 0.0f); // red
            OpenGL.glVertex3f(0.0f, 1.0f, 0.0f); // top of triangle (back)
            OpenGL.glColor3f(0.0f, 1.0f, 0.0f); // green
            OpenGL.glVertex3f(1.0f, -1.0f, -1.0f); // left of triangle (back)
            OpenGL.glColor3f(0.0f, 0.0f, 1.0f); // blue
            OpenGL.glVertex3f(-1.0f, -1.0f, -1.0f); // right of triangle (back)

            OpenGL.glColor3f(1.0f, 0.0f, 0.0f); // red
            OpenGL.glVertex3f(0.0f, 1.0f, 0.0f); // top of triangle (left)
            OpenGL.glColor3f(0.0f, 0.0f, 1.0f); // blue
            OpenGL.glVertex3f(-1.0f, -1.0f, -1.0f); // left of triangle (left)
            OpenGL.glColor3f(0.0f, 1.0f, 0.0f); // green
            OpenGL.glVertex3f(-1.0f, -1.0f, 1.0f); // right of triangle (left)
            OpenGL.glEnd();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Size s = Size;

            if (s.Height == 0)
                s.Height = 1;

            OpenGL.glViewport(0, 0, s.Width, s.Height);

            OpenGL.glMatrixMode(OpenGL.GL_PROJECTION);
            OpenGL.glLoadIdentity();
            GLU.gluPerspective(45.0f, s.Width / (double)s.Height, 0.1f, 100000.0f);
            OpenGL.glMatrixMode(OpenGL.GL_MODELVIEW);
            OpenGL.glLoadIdentity();
        }

        protected void LessonView_KeyDown(object Sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    Finished = true;
                    break;

                default:
                    if (e.KeyCode == Keys.F && !Fp) // On the F key, cycle the texture filter (texture used)
                    {
                        Fp = true;
                        Filter = (Filter + 1) % 3;
                    }
                    else if (e.KeyCode == Keys.B && !Bp) // Blending code starts here
                    {
                        Bp = true;
                        Blend = !Blend;
                        if (Blend)
                        {
                            OpenGL.glEnable(OpenGL.GL_BLEND); // Turn Blending On
                            OpenGL.glDisable(OpenGL.GL_DEPTH_TEST); // Turn Depth Testing Off
                        }
                        else
                        {
                            OpenGL.glDisable(OpenGL.GL_BLEND); // Turn Blending Off
                            OpenGL.glEnable(OpenGL.GL_DEPTH_TEST); // Turn Depth Testing On
                        }
                    } // Blending Code Ends Here
                    else if (e.KeyCode == Keys.PageUp) // On page up, move out
                    {
                        Z -= 0.02f;
                        Lookupdown -= 1.0f;
                    }
                    else if (e.KeyCode == Keys.PageDown) // On page down, move in
                    {
                        Z += 0.02f;
                        Lookupdown += 1.0f;
                    }
                    break;
            }
        }

        private void LessonView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F) // Release the filter cycle key lock
                Fp = false;
            else if (e.KeyCode == Keys.B) // Release the blending toggle key lock
                Bp = false;
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.D:
                    _cc.MoveForwardOrBack(-MoveOnXz);
                    break;
                case Keys.E:
                    _cc.MoveForwardOrBack(MoveOnXz);
                    Debug.WriteLine(_cc);
                    break;
                case Keys.S:
                    _cc.MoveLeftOrRight(MoveOnXz);
                    break;
                case Keys.F:
                    _cc.MoveLeftOrRight(-MoveOnXz);
                    break;
                case Keys.PageUp:
                    _cc.MoveUpOrDown(MoveOnY);
                    break;
                case Keys.PageDown:
                    _cc.MoveUpOrDown(-MoveOnY);
                    break;
                case Keys.Space:
                    _cc.MoveUpOrDown(MoveOnY);
                    break;
            }

            return base.ProcessDialogKey(keyData);
        }


        /// <summary>
        ///     画坐标系
        /// </summary>
        private void DrawCoords()
        {
            OpenGL.glBegin(OpenGL.GL_LINES);
            OpenGL.glColor3b(sbyte.MaxValue, 0, 0);
            OpenGL.glVertex3f(0, 0, 0);
            OpenGL.glVertex3f(100000, 0, 0);
            OpenGL.glColor3b(0, sbyte.MaxValue, 0);
            OpenGL.glVertex3f(0, 0, 0);
            OpenGL.glVertex3f(0, 100000, 0);
            OpenGL.glColor3b(0, 0, sbyte.MaxValue);
            OpenGL.glVertex3f(0, 0, 0);
            OpenGL.glVertex3f(0, 0, 100000);
            OpenGL.glEnd();
        }

        private void DrawMeshes()
        {
            for (int m = 0; m < _meshes.Count; m++)
            {
                for (int c = 0; c < _meshes[m].Count; c++)
                {
                    OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, 1);
                    OpenGL.glBegin(OpenGL.GL_TRIANGLES);
                    OpenGL.glNormal3f(0.0f, 0.0f, 1.0f);

                    for (int t = 0; t < _meshes[m][c]._idata.Length / 3; t++)
                    {
                        uint t1 = _meshes[m][c]._idata[3 * t];
                        uint t2 = _meshes[m][c]._idata[3 * t + 1];
                        uint t3 = _meshes[m][c]._idata[3 * t + 2];


                        Point3D p1 = _meshes[m][c]._vdata[t1];
                        Point3D p2 = _meshes[m][c]._vdata[t2];
                        Point3D p3 = _meshes[m][c]._vdata[t3];
                        Point2D u1 = _meshes[m][c]._udata[t1];
                        Point2D u2 = _meshes[m][c]._udata[t2];
                        Point2D u3 = _meshes[m][c]._udata[t3];

                        OpenGL.glTexCoord2d(u1.X, u1.Y);
                        OpenGL.glVertex3d(p1.X, p1.Y, p1.Z);
                        OpenGL.glTexCoord2d(u2.X, u2.Y);
                        OpenGL.glVertex3d(p2.X, p2.Y, p2.Z);
                        OpenGL.glTexCoord2d(u3.X, u3.Y);
                        OpenGL.glVertex3d(p3.X, p3.Y, p3.Z);
                    }
                    OpenGL.glEnd();
                }
            }
        }

        private void DrawTriangles()
        {
            for (int inx = 0; inx < Texfile.Count; inx++)
            {
                if (Texfile[inx].Length > 5 && Texfile[inx].Substring(0, 5).ToUpper() == "COLOR")
                {
                    OpenGL.glColor3f(0.6f, 0.6f, 0.6f);
                    OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, 1000);
                }
                else
                {
                    OpenGL.glColor3f(0.4f, 0.4f, 0.4f);
                    OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, SceneTexs[Texfile[inx]]);
                }
                OpenGL.glBegin(OpenGL.GL_TRIANGLES);

                for (var i = 0; i < _triangles5D.Length; i++)
                {

                    if (_triangles5D[i].Material != Texfile[inx])
                    {
                        continue;
                    }
                    var t = _triangles5D[i];
                    var p1 = t.Vertexs[0];
                    var p2 = t.Vertexs[1];
                    var p3 = t.Vertexs[2];

                    OpenGL.glTexCoord2d(p1.U, p1.V);
                    OpenGL.glNormal3d(p1.NormalsVector.X, p1.NormalsVector.Y, p1.NormalsVector.Z);
                    OpenGL.glVertex3d(p1.X, p1.Y, p1.Z);

                    OpenGL.glTexCoord2d(p2.U, p2.V);
                    OpenGL.glNormal3d(p2.NormalsVector.X, p2.NormalsVector.Y, p2.NormalsVector.Z);
                    OpenGL.glVertex3d(p2.X, p2.Y, p2.Z);

                    OpenGL.glTexCoord2d(p3.U, p3.V);
                    OpenGL.glNormal3d(p3.NormalsVector.X, p3.NormalsVector.Y, p3.NormalsVector.Z);
                    OpenGL.glVertex3d(p3.X, p3.Y, p3.Z);
                }
                OpenGL.glEnd();
            }
        }

        private void DrawFaces()
        {
        }

        private unsafe void DrawTerrain()
        {
             
        }




        public HeightMap hm;

        private void DrawHeightmap()
        {
            //OpenGL.glLoadIdentity();
            OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, SceneTexs[hm.TexStr]);
            GL.glDrawElements(GL.GL_TRIANGLES, indexlength, GL.GL_UNSIGNED_INT, indices);

        }

        public IntPtr indices;
        public int indexlength;

        public void SetupPointers()
        {

            if (!Texfile.Contains(hm.TexStr))
            {
                Texfile.Add(hm.TexStr);
            }

            LoadTextures(Texfile, Texpath);

            double[] v = hm.Triangle5Ds.Vertices.ToDoubleArray();
            double[] t = hm.Triangle5Ds.Texcoords.ToDoubleArray();
            uint[] i = hm.Triangle5Ds.Indices.ToArray();
            double[] n = hm.Triangle5Ds.Normals.ToDoubleArray();



            GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
            GL.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
            GL.glEnableClientState(GL.GL_INDEX_ARRAY);
            GL.glEnableClientState(GL.GL_NORMAL_ARRAY);

            GlHelper.glVertexPointer(3, GL.GL_DOUBLE, 0, v);
            GlHelper.glTexCoordPointer(2, GL.GL_DOUBLE, 0, t);
            GlHelper.glNormalPointer(GL.GL_DOUBLE, 0, n);
            GCHandle verObj = GCHandle.Alloc(i, GCHandleType.Pinned);
            IntPtr vObj = verObj.AddrOfPinnedObject();
            indices = vObj;
            indexlength = i.Length;
            //GlHelper.glIndexPointer(GL.GL_INT,0,i);

        }

        public SkyDome sd;
        private IntPtr Skyindices;
        private int Skyindexlength;
        private double rotate = 0;
        private void DrawSky()
        {
            OpenGL.glLoadIdentity();
            if (rotate > 1000)
            {
                rotate = 0;
            }
            rotate += 0.01;
            GL.glRotated(rotate, 0, 1, 0);
            GL.glTranslated(-_cc.CameraLoc.X, 0, -_cc.CameraLoc.Z);
            OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, SceneTexs[sd.DomeTex]);
            OpenGL.glBegin(OpenGL.GL_TRIANGLES);
            //OpenGL.glNormal3f(0.0f, 0.0f, 1.0f);
            for (int i = 0; i < sd.Triangle5Ds.Length; i++)
            {
                var t = sd.Triangle5Ds[i];
                var p1 = t.Vertexs[0];
                var p2 = t.Vertexs[1];
                var p3 = t.Vertexs[2];

                OpenGL.glTexCoord2d(p1.U, p1.V);
                OpenGL.glNormal3d(p1.NormalsVector.X, p1.NormalsVector.Y, p1.NormalsVector.Z);
                OpenGL.glVertex3d(p1.X, p1.Y, p1.Z);

                OpenGL.glTexCoord2d(p2.U, p2.V);
                OpenGL.glNormal3d(p2.NormalsVector.X, p2.NormalsVector.Y, p2.NormalsVector.Z);
                OpenGL.glVertex3d(p2.X, p2.Y, p2.Z);

                OpenGL.glTexCoord2d(p3.U, p3.V);
                OpenGL.glNormal3d(p3.NormalsVector.X, p3.NormalsVector.Y, p3.NormalsVector.Z);
                OpenGL.glVertex3d(p3.X, p3.Y, p3.Z);
            }
            OpenGL.glEnd();

        }



    }
}